package com.dilean.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.dilean.api.dao.AttributeDAO;
import com.dilean.api.dao.AttributeListDAO;
import com.dilean.api.dao.AttributeListValueDAO;
import com.dilean.api.dao.AttributeTypeDAO;
import com.dilean.api.dao.ConversationDAO;
import com.dilean.api.dao.MessageDAO;
import com.dilean.api.dao.PersonDAO;
import com.dilean.api.model.AttributeValueFactory;
import com.dilean.common.api.DataImporter;
import com.dilean.common.api.DileanMailSender;
import com.dilean.common.api.Properties;
import com.dilean.common.impl.DataImporterExcel;
import com.dilean.common.impl.DbDileanPrioperties;
import com.dilean.common.impl.DileanMailSenderImpl;
import com.dilean.impl.dao.AttributeDAOImpl;
import com.dilean.impl.dao.AttributeListDAOImpl;
import com.dilean.impl.dao.AttributeListValueDAOImpl;
import com.dilean.impl.dao.AttributeTypeDAOImpl;
import com.dilean.impl.dao.ConversationDAOImpl;
import com.dilean.impl.dao.MessageDAOImpl;
import com.dilean.impl.dao.PersonDAOImpl;
import com.dilean.impl.model.attrval.AttrValueFactoryImpl;
import com.dilean.spring.jpa.PersistenceJpaConfig;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;

@Configuration
@Import(value = { PersistenceJpaConfig.class })
public class AplicationConfig
{
    @Bean 
    public PersonDAO getPersonDAO()
    {
        return new PersonDAOImpl();
    }
    
    @Bean 
    public ConversationDAO getConversationDAO()
    {
        return new ConversationDAOImpl();
    }
    
    @Bean 
    public MessageDAO getMessageDAO()
    {
        return new MessageDAOImpl();
    }

    @Bean 
    public AttributeTypeDAO getAttributeTypeDAO()
    {
        return new AttributeTypeDAOImpl();
    }

    @Bean 
    public AttributeListDAO getAttributeListDAO()
    {
        return new AttributeListDAOImpl();
    }
    
    @Bean 
    public AttributeDAO getAttributeDAO()
    {
        return new AttributeDAOImpl();
    }
    
    @Bean 
    public AttributeListValueDAO getAttributeListValueDAO()
    {
        return new AttributeListValueDAOImpl();
    }
    

    @Bean 
    public AttributeValueFactory getAttributeValueFactory()
    {
        return new AttrValueFactoryImpl();
    }
    
    @Bean 
    public DataImporter getExcelConverter()
    {
        return new DataImporterExcel();
    }
    
    @Bean
    public Properties getProperties()
    {
    	return new DbDileanPrioperties();
    }
    
    @Bean
    public JavaMailSender mailSender()
    {
    	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    	mailSender.setHost("smtp.gmail.com");
    	mailSender.setPort(587);
    	mailSender.setUsername("dileanMail@gmail.com");
    	mailSender.setPassword("dilean1234");
 
    	final java.util.Properties javaMailProperties = new java.util.Properties();
        
        try
        {
        	javaMailProperties.load(PersistenceJpaConfig.class.getResourceAsStream("mail.properties"));
        }
        catch(Exception ex)
        {
            throw new RuntimeException("Cannot load mail.properties", ex);
        }
    	mailSender.setJavaMailProperties(javaMailProperties);
    	return mailSender;
    }
    
    @Bean 
    public DileanMailSender getDileanMailSender()
    {
        return new DileanMailSenderImpl();
    }
    
    @Bean 
    public VelocityEngineFactoryBean getVelocityEngineFactoryBean()
    {
    	VelocityEngineFactoryBean velocityEngine = new VelocityEngineFactoryBean();
    	java.util.Properties velocityProperties = new java.util.Properties();
    	velocityProperties.put("resource.loader", "class");
    	velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    	velocityEngine.setVelocityProperties(velocityProperties);
        return velocityEngine;
    }
}
